﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
	public partial class CurrentGoodsList : BasePage
	{
		private string strMosth = "";//月份
		//GroupByField groupField = GroupByField.PurchaseTimeMonth | GroupByField.SortSource | GroupByField.SortID;
        GroupByField groupField = GroupByField.PurchaseTimeMonth | GroupByField.SortID;
		string sortField = "PurchaseTimeMonth ASC";
		string append = "";
		bool hide = true;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{

				ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
				SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
				strMosth = String.IsNullOrEmpty(sq.Mid) ? "" : sq.Mid;
				if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
				{
                    if (sq.SortSource == 0)
                    {
                        droSort.SelectedIndex = 1;
                    }
                    else if (sq.SortSource == 1)
                    {
                        droSort.SelectedIndex = 2;
                    }
                    else if (sq.SortSource == 2)
                    {
                        droSort.SelectedIndex = 3;
                    }
                    else
                    {
                        droSort.SelectedIndex = -1;
                    }
					BindList();
					GetStatistics();
				}
				else
				{
					Response.Write("<script>history.go(-1);<script>");
				}
			}
		}



		/// <summary>
		/// 统计当前的采购和入库总金额
		/// </summary>
		void GetStatistics()
		{
			double purchaseAmountTotal = 0;//采购总金额
			double wareHouseAmountTotal = 0;//入库总金额
			double distributeAmountTotal = 0;//配送总金额
			for (int i = 0; i < gvwSortInfo.Rows.Count; i++)
			{
				purchaseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[2].Text);
				wareHouseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[5].Text);
				distributeAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[3].Text);
			}
			this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributeAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString();

		}
		void BindList()
		{
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

			var model = new HZ.Data.QueryModel();
			model["HospitalName"] = sq.HospitalName;
			model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

			model["SortFields"] = this.droSortField.SelectedValue;

			int t = 0;

			HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

			System.Data.DataTable dt = iNew.DayTime_SortSorce_GroupByForHosCurrentGoodsList(model, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
			AspNetPager1.RecordCount = t;


			gvwSortInfo.DataSource = dt;
			HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
			HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
			gvwSortInfo.DataBind();
			HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 0);
			HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 7);
		}

		/// <summary>
		/// 合并表头
		/// 张雄 modify 2011.4.26
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
		{

			#region 合并表头
			switch (e.Row.RowType)
			{
				case DataControlRowType.Header:

					//取得下拉框的月份
					//第一行表头
					TableCellCollection tcHeader = e.Row.Cells;
					tcHeader.Clear();
					tcHeader.Add(new TableHeaderCell());
					tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
					tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
					SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
					tcHeader[0].Text = ForMatDate(sq.Mid) + " " + sq.HospitalName + "</th></tr><tr>";

					//第2行表头
					tcHeader.Add(new TableHeaderCell());
					tcHeader[1].Attributes.Add("bgcolor", "Khaki");
					tcHeader[1].Text = "医院名称";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[2].Attributes.Add("bgcolor", "Khaki");
					tcHeader[2].Text = "采购类型";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[3].Attributes.Add("bgcolor", "Khaki");
					tcHeader[3].Text = "采购总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[4].Attributes.Add("bgcolor", "Khaki");
					tcHeader[4].Text = "配送总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[5].Attributes.Add("bgcolor", "Khaki");
					tcHeader[5].Text = "配送率";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[6].Attributes.Add("bgcolor", "Khaki");
					tcHeader[6].Text = "入库总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[7].Attributes.Add("bgcolor", "Khaki");
					tcHeader[7].Text = "入库率";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[8].Attributes.Add("bgcolor", "Khaki");
					tcHeader[8].Text = "操作</th></tr><tr>";
					break;
			}
			#endregion
		}

		/// <summary>
		/// 格式化日期
		/// 张雄 create 2011.4.26
		/// </summary>
		/// <param name="Str">日期参数</param>
		/// <returns>格式化后的日期</returns>
		private string ForMatDate(string Str)
		{
			string YearStr = String.Empty;

			string MonStr = String.Empty;

			if (!String.IsNullOrEmpty(Str))
			{
				if (Str.Length == 6)
				{
					//取得年
					YearStr = Str.Substring(0, 4) + "年";
					//取得月
					MonStr = Str.Substring(4, 2);
					if (MonStr.Substring(0, 1) == "0")
					{
						MonStr = MonStr.Substring(1, 1) + "月";
					}
					else
					{
						MonStr += "月";
					}
				}
			}
			return Str = YearStr + MonStr;
		}

		protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
		{

		}

		protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "DirectByDetail")
			{
				string sessionid = Guid.NewGuid().ToString();
				SuperviseQuery query = (SuperviseQuery)ViewState["query"];
 
				//query.SortSource = Convert.ToInt32(this.hidSortSource.Value);
                //把当前的drop状态传入Session
                query.SortSource = Convert.ToInt32(this.droSort.SelectedValue);
				Session[sessionid] = query;
				string url = "HospitalGoodsList.aspx";
				Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
			}
		}


		protected void btnQuery_Click(object sender, EventArgs e)
		{
			if (!this.droSort.SelectedValue.Equals("-1"))
			{
				append = "AND SortSorce = " + droSort.SelectedValue;
			}
			else
			{
				groupField = GroupByField.PurchaseTimeMonth | GroupByField.SortID;
				hide = false;
			}
			sortField = this.droSortField.SelectedValue;
			this.hidSortSource.Value = this.droSort.SelectedValue.ToString();
			BindList();
			GetStatistics();
		}




		/// <summary>
		/// 导出按钮
		/// by张雄  2011.5.13
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void btnExcel_Click(object sender, EventArgs e)
		{
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

			var model = new HZ.Data.QueryModel();
			model["HospitalName"] = sq.HospitalName;
			model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

			model["SortFields"] = this.droSortField.SelectedValue;

			int t = 0;

			HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

			System.Data.DataTable dt = iNew.DayTime_SortSorce_GroupByForHosCurrentGoodsList(model, 1, 0, out t);
			gvwSortInfo.DataSource = dt;
			HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
			HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
			gvwSortInfo.DataBind();

			HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwSortInfo);
		}

		/// <summary>
		/// 分页    by张雄
		/// </summary>
		/// <param name="sender">事件的发送源</param>
		/// <param name="e">事件参数</param>
		protected void AspNetPager1_PageChanged(object sender, EventArgs e)
		{
			BindList();
		}
	}
}
